1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
<script lang="ts">
import { userIdentity } from '$lib/AniList/identity.js';
import { user } from '$lib/AniList/user.js';
import { nbsp } from '$lib/Tools/tool';
import type { Badge } from '$lib/Database/badges';
import { domToBlob } from 'modern-screenshot';
import { onMount } from 'svelte';
// import { io } from 'socket.io-client';
export let data;
let editMode = false;
let currentUserIdentity: ReturnType<typeof userIdentity>;
let error: null | string;
// const socket = io();
let badges: Badge[] | null = null;
let dark = true;
let transparent = false;
onMount(async () => {
// socket.on('badges', (message) => (badges = message));
badges = await (await fetch(`/api/badges?id=${(await user(data.username)).id}`)).json();
if (data.user) {
currentUserIdentity = userIdentity(data.user);
// socket.emit('badges', data.user);
} else {
currentUserIdentity = new Promise((resolve) =>
resolve({
name: 'Guest',
id: -1
})
);
}
});
const submitBadge = () => {
const imageURL = document.querySelector('input[name="image_url"]') as HTMLInputElement;
const activityURL = document.querySelector('input[name="activity_url"]') as HTMLInputElement;
const description = document.querySelector('input[name="description"]') as HTMLInputElement;
if (!imageURL.value || !activityURL.value) {
error = 'Fields cannot be empty.';
return;
}
if (!imageURL.value.startsWith('http') || !activityURL.value.startsWith('http')) {
error = 'URLs must start with http or https.';
return;
}
fetch(
`/api/badges?image=${encodeURIComponent(imageURL.value)}&post=${encodeURIComponent(
activityURL.value
)}&description=${encodeURIComponent(description.value)}`,
{
method: 'PUT'
}
).then(() => {
error = null;
imageURL.value = '';
activityURL.value = '';
description.value = '';
});
};
const removeBadge = (badge: Badge) => {
fetch(`/api/badges?id=${badge.id}`, {
method: 'DELETE'
}).then(() => {
(document.querySelector(`#badge-${badge.id}`) as HTMLAnchorElement).style.display = 'none';
});
};
const screenshot = async () => {
let element = document.querySelector('#badges') as HTMLElement;
element.classList.add('invert');
if (element !== null) {
domToBlob(element, {
style: {
backgroundColor: transparent ? 'transparent' : dark ? '#0b1622' : '#edf1f5'
},
quality: 1,
scale: 2,
fetch: {
requestInit: {
mode: 'cors'
}
// bypassingCache: true
}
}).then((blob) => {
const downloadWrapper = document.createElement('a');
const wrappedImageButton = document.getElementById(
'wrapped-image-download'
) as HTMLAnchorElement;
const image = document.createElement('img');
const object = (window.URL || window.webkitURL || window || {}).createObjectURL(blob);
downloadWrapper.href = object;
downloadWrapper.target = '_blank';
image.src = object;
downloadWrapper.appendChild(image);
if (wrappedImageButton !== null) {
wrappedImageButton.href = object;
}
const wrappedFinal = document.getElementById('wrapped-final');
if (wrappedFinal !== null) {
wrappedFinal.innerHTML = '';
wrappedFinal.appendChild(downloadWrapper);
}
downloadWrapper.click();
});
await new Promise((resolve) => setTimeout(resolve, 1000));
element.classList.remove('invert');
}
};
</script>
{#await currentUserIdentity}
Loading user identity ... 50%
{:then identity}
{@const isOwner = identity && identity.name === data.username}
{#if isOwner}
<p>
<a href={`/user/${data.username}`}>Back to Profile</a>
•
<a href={`#`} on:click={() => (editMode = !editMode)}>
{editMode ? 'Disable' : 'Enable'} Edit Mode
</a>
•
<a href={`#`} on:click={() => screenshot()}>Download</a>
•
<input type="checkbox" bind:checked={dark} /> Dark Mode
<input type="checkbox" bind:checked={transparent} /> Transparent Background
</p>
{/if}
{#if editMode && isOwner}
<p>
Delete mode is enabled. Click on an image to delete it. There is no confirmation, so be
careful!
</p>
{#if error}
<p style="color: red;">{error}</p>
{/if}
<p>
<input type="text" placeholder="Image URL" name="image_url" minlength="1" maxlength="1000" />
<input
type="text"
placeholder="Activity URL"
name="activity_url"
minlength="1"
maxlength="1000"
/>
<input
type="text"
placeholder="Description (Optional)"
name="description"
minlength="1"
maxlength="1000"
/>
<a href={`#`} on:click={submitBadge}>Add Badge</a>
</p>
{/if}
<div id="badges">
{#if badges === null}
{@html nbsp('Loading badges ... 50%')}
{:else if badges.length === 0}
{@html nbsp('No badges found for this user.')}
{:else}
{#each badges as badge}
{#if editMode}
<a href={`#`} on:click={() => removeBadge(badge)} id={`badge-${badge.id}`}>
<img src={badge.image} alt={badge.description} title={badge.description} />
</a>
{:else}
<a href={badge.post} target="_blank" id={`badge-${badge.id}`}>
<img src={badge.image} alt={badge.description} title={badge.description} />
</a>
{/if}
{/each}
{/if}
</div>
{/await}
<style>
/* body {
margin: 0;
padding: 0;
text-align: center;
background-color: #151f2e;
} */
img {
width: 100%;
height: auto;
}
#badges {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(8%, 1fr));
grid-gap: 0.25%;
}
:global(.invert *) {
filter: invert(0) !important;
}
</style>
|